﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using Ultra_Presenter.Model;
using Ultra_Presenter.UC;
using Ultra_Presenter.Util;

namespace Ultra_Presenter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Project object.
        /// </summary>
        private Model.Project project;

        /// <summary>
        /// Verify if window is activated.
        /// </summary>
        private bool isWindowActived = false;

        /// <summary>
        /// Layout list.
        /// </summary>
        private ArrayList lstLayouts;

        /// <summary>
        /// Dashbord list layout.
        /// </summary>
        private List<Dashboard> lstDashBoardTimeLine;

        /// <summary>
        /// Object temporary layout.
        /// </summary>
        private object layout;

        /// <summary>
        /// Dashboard object.
        /// </summary>
        private Model.Dashboard dash1;

        /// <summary>
        /// Dashboard object.
        /// </summary>
        private Model.Dashboard dash2;

        /// <summary>
        /// Dashboard object.
        /// </summary>
        private Model.Dashboard dash3;

        /// <summary>
        /// Dashboard object.
        /// </summary>
        private Model.Dashboard dash4;

        /// <summary>
        /// Dashboard object.
        /// </summary>
        private Model.Dashboard dash5;

        /// <summary>
        /// Dashboard object.
        /// </summary>
        private Model.Dashboard dash6;

        /// <summary>
        /// Animante the menu.
        /// </summary>
        /// <param name="sender">Object to animante.</param>
        private static void AnimateMenu(object sender)
        {
            ////TabItem t = (TabItem)sender;
            ////DoubleAnimation a = new DoubleAnimation();
            ////a.From = 1;
            ////a.To = 0;
            ////a.Duration = TimeSpan.FromSeconds(0.1);
            ////a.AutoReverse = true;
            ////t.BeginAnimation(TabItem.OpacityProperty, a);
        }

        /// <summary>
        /// Class constructor.
        /// </summary>
        public MainWindow()
        {
            this.InitializeComponent();

            System.Drawing.Bitmap icon = Properties.Resources.Time_Machine.ToBitmap();

            MemoryStream iconStream = new MemoryStream();
            icon.Save(iconStream, System.Drawing.Imaging.ImageFormat.Png);
            iconStream.Seek(0, SeekOrigin.Begin);
            this.Icon = BitmapFrame.Create(iconStream);
            this.webShow.Navigate("http://marcio-pulcinelli.com/ultrapresenter/");
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L2_ChangedA(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L2-A";
            this.dgURLsDashboadr.ItemsSource = this.L2.GetURLs(QuadrantName.A);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L1_ChangedD(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L1-D";
            this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.D);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L1_ChangedC(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L1-C";
            this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.C);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L1_ChangedB(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L1-B";
            this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.B);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L1_ChangedA(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L1-A";
            this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.A);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L1_TL_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.DragDrop.DoDragDrop(this.L1_TL, sender, DragDropEffects.Copy);
            this.layout = new Layout_1();
        }

        /// <summary>
        /// Mouse drag event .
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void Rectangle_Drop(object sender, DragEventArgs e)
        {
            this.AddDashboardToTimeline();
        }

        /// <summary>
        /// Method to add dashboards to the timeline.
        /// </summary>
        private void AddDashboardToTimeline()
        {
            if (this.lstPicture.Items.Count <= 4)
            {
                if (this.VerifyLayoutContent(this.layout.ToString(), true))
                {
                    this.lstLayouts.Add(this.layout);
                    this.lstPicture.ItemsSource = null;
                    this.lstPicture.ItemsSource = this.lstLayouts;

                    this.lstDashBoardTimeLine.Add(new Dashboard { Name = this.layout.ToString() });
                    this.project.TimeLine.Dashboards = this.lstDashBoardTimeLine.ToArray();
                }
            }
            else
            {
                MessageBox.Show("Limit exceded!");
            }
        }

        /// <summary>
        /// Verify the layout content.
        /// </summary>
        /// <param name="nome">Layout name.</param>
        /// <param name="showmsg">Shows messagebox.</param>
        /// <returns>True - if everything goes right.</returns>
        private bool VerifyLayoutContent(string nome, bool showmsg)
        {
            Dashboard dashboard = this.project.Dashboards.Where(x => x.Name.Equals(nome)).FirstOrDefault();

            if (dashboard == null)
            {
                if (showmsg)
                {
                    MessageBox.Show("The layout you are trying to add doen't have any information.");
                }

                return false;
            }
            else if (dashboard.Quadrants == null)
            {
                if (showmsg)
                {
                    MessageBox.Show("No quadrant has information.");
                }

                return false;
            }
            else
            {
                foreach (var item in dashboard.Quadrants)
                {
                    if (item.Urls == null || item.Urls.Length <= 0)
                    {
                        if (showmsg)
                        {
                            MessageBox.Show(string.Format("The quadrant '{0}' has no data.", item.Name.ToString()));
                        }

                        return false;
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// Mouse move event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L2_TL_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.DragDrop.DoDragDrop(this.L2_TL, sender, DragDropEffects.Link);
            this.layout = new Layout_2();
        }

        /// <summary>
        /// Mouse move event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L3_TL_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.DragDrop.DoDragDrop(this.L3_TL, sender, DragDropEffects.Link);
            this.layout = new Layout_3();
        }

        /// <summary>
        /// Mouse move event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L4_TL_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.DragDrop.DoDragDrop(this.L4_TL, sender, DragDropEffects.Link);
            this.layout = new Layout_4();
        }

        /// <summary>
        /// Mouse move event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L5_TL_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.DragDrop.DoDragDrop(this.L5_TL, sender, DragDropEffects.Link);
            this.layout = new Layout_5();
        }

        /// <summary>
        /// Mouse move event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L6_TL_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.DragDrop.DoDragDrop(this.L6_TL, sender, DragDropEffects.Link);
            this.layout = new Layout_6();
        }

        /// <summary>
        /// Mouse click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnRemoveDashBoard_Click(object sender, RoutedEventArgs e)
        {
            int index = this.lstPicture.SelectedIndex;

            if (index > -1)
            {
                this.lstLayouts.RemoveAt(index);

                this.lstPicture.ItemsSource = null;
                this.lstPicture.ItemsSource = this.lstLayouts;

                this.lstDashBoardTimeLine.RemoveAt(index);
                this.project.TimeLine.Dashboards = this.lstDashBoardTimeLine.ToArray();
            }
        }

        /// <summary>
        /// Mouse click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnAddURL_Click(object sender, RoutedEventArgs e)
        {
            if (this.txtURL.Text.Equals(string.Empty))
            {
                MessageBox.Show(this, "URL name is missing!", Properties.Resources.SysName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                this.txtURL.Focus();
                return;
            }

            Model.Url url = new Model.Url
            {
                Name = this.txtURL.Text
            };

            List<Model.Url> lstUrl;

            if (this.project.Urls != null)
            {
                lstUrl = this.project.Urls.ToList<Model.Url>();
            }
            else
            {
                lstUrl = new List<Model.Url>();
            }

            lstUrl.Add(url);
            this.project.Urls = lstUrl.ToArray();

            this.dgURLs.ItemsSource = this.project.Urls;

            this.txtURL.Text = string.Empty;
            this.txtURL.Focus();
        }

        /// <summary>
        /// Mouse leftbuttonup event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void TabSetupDashBoard_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AnimateMenu(sender);
            this.cboURLs.DisplayMemberPath = "Name";
            this.cboURLs.ItemsSource = this.project.Urls;
        }

        /// <summary>
        /// Method to add data to the project object.
        /// </summary>
        private void SaveDataToProject()
        {
            this.project.Name = this.txtProjectName.Text.Trim();
            this.project.UserName = this.txtUserName.Text.Trim();
        }

        /// <summary>
        /// Mouse click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.txtProjectName.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show(this, "Project name is missing!", Properties.Resources.SysName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                this.txtProjectName.Focus();
                return;
            }

            this.LoadWaiting();

            this.SaveDataToProject();

            if (this.txtProjectPath.Text.Trim().Equals(string.Empty))
            {
                SaveFileDialog sd = new SaveFileDialog();
                sd.Filter = "Ultra Presenter|*.xml";
                sd.Title = "Save your project to a file...";
                sd.FileName = this.txtProjectName.Text + ".xml";

                if ((bool)sd.ShowDialog())
                {
                    if (sd.FileName != string.Empty)
                    {
                        this.txtProjectPath.Text = sd.FileName;
                        SerializationHelper.Serialize(this.project, sd.FileName);
                    }
                }
            }
            else
            {
                SerializationHelper.Serialize(this.project, this.txtProjectPath.Text);
            }

            this.LoadWaiting();
        }

        /// <summary>
        /// Method to load project into memory.
        /// </summary>
        private void LoadProject()
        {
            this.txtProjectName.Text = this.project.Name;
            this.txtUserName.Text = this.project.UserName;
            this.txtProjectNameSummary.Text = this.project.Name;
            this.txtUserNameSummary.Text = this.project.UserName;

            this.dgURLs.ItemsSource = this.project.Urls;

            foreach (var db in this.project.Dashboards)
            {
                switch (db.Name)
                {
                    case "Ultra_Presenter.UC.Layout_1":

                        foreach (var qd in db.Quadrants)
                        {
                            this.L1.SetQuadrant(qd);
                        }

                        break;

                    case "Ultra_Presenter.UC.Layout_2":
                        foreach (var qd in db.Quadrants)
                        {
                            this.L2.SetQuadrant(qd);
                        }

                        break;
                    case "Ultra_Presenter.UC.Layout_3":
                        foreach (var qd in db.Quadrants)
                        {
                            this.L3.SetQuadrant(qd);
                        }

                        break;
                    case "Ultra_Presenter.UC.Layout_4":
                        foreach (var qd in db.Quadrants)
                        {
                            this.L4.SetQuadrant(qd);
                        }

                        break;
                    case "Ultra_Presenter.UC.Layout_5":
                        foreach (var qd in db.Quadrants)
                        {
                            this.L5.SetQuadrant(qd);
                        }

                        break;
                    case "Ultra_Presenter.UC.Layout_6":
                        foreach (var qd in db.Quadrants)
                        {
                            this.L6.SetQuadrant(qd);
                        }

                        break;

                    default:
                        break;
                }
            }

            if (this.project.TimeLine.Dashboards != null)
            {
                foreach (var item in this.project.TimeLine.Dashboards)
                {
                    Type tp = Type.GetType(item.Name);
                    object lay = Activator.CreateInstance(tp);
                    this.lstLayouts.Add(lay);
                    this.lstPicture.ItemsSource = null;
                    this.lstPicture.ItemsSource = this.lstLayouts;

                    this.lstDashBoardTimeLine.Add(new Dashboard { Name = lay.ToString() });
                    this.project.TimeLine.Dashboards = this.lstDashBoardTimeLine.ToArray();
                }
            }
        }

        /// <summary>
        /// Mouse click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnOpen_Click(object sender, RoutedEventArgs e)
        {
            this.LoadWaiting();

            OpenFileDialog od = new OpenFileDialog();
            od.Filter = "Ultra Presenter|*.xml";
            od.Title = "Open your project...";
            od.FileName = string.Empty;

            if ((bool)od.ShowDialog())
            {
                try
                {
                    this.InitializeNewProject();
                    this.project = SerializationHelper.Deserialize(od.FileName);
                    this.txtProjectPath.Text = od.FileName;
                    this.LoadProject();
                    this.LoadWaiting();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, Properties.Resources.SysName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    this.LoadWaiting();
                }
            }
            else
            {
                this.LoadWaiting();
            }
        }

        /// <summary>
        /// Method to show wait message.
        /// </summary>
        private void LoadWaiting()
        {
            if (this.overLap.Visibility == System.Windows.Visibility.Visible)
            {
                this.webShow.Visibility = System.Windows.Visibility.Visible;
                this.overLap.Visibility = System.Windows.Visibility.Hidden;
            }
            else
            {
                this.webShow.Visibility = System.Windows.Visibility.Hidden;
                this.overLap.Visibility = System.Windows.Visibility.Visible;
            }

            this.UpdateLayout();
        }

        /// <summary>
        /// Mouse click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnNew_Click(object sender, RoutedEventArgs e)
        {
            this.LoadWaiting();
            this.InitializeNewProject();
            this.LoadWaiting();
        }

        /// <summary>
        /// Method to initialize new project.
        /// </summary>
        private void InitializeNewProject()
        {
            this.project = new Model.Project();
            this.project.TimeLine = new TimeLine();

            this.lstDashBoardTimeLine = new List<Dashboard>();
            this.lstLayouts = new ArrayList();

            this.txtProjectName.Text = this.project.Name;
            this.txtUserName.Text = System.Environment.UserName;
            this.txtProjectPath.Text = string.Empty;

            this.dgURLs.ItemsSource = null;
            this.dgURLsDashboadr.ItemsSource = null;
            this.lstPicture.ItemsSource = null;

            this.lstPathSummary_Copy.ItemsSource = null;
            this.lstPathSummary.ItemsSource = null;

            this.L1.ResetControl();
            this.L2.ResetControl();
            this.L3.ResetControl();
            this.L4.ResetControl();
            this.L5.ResetControl();
            this.L6.ResetControl();

            List<Dashboard> db = new List<Dashboard>();

            this.dash1 = new Dashboard
            {
                Name = this.L1.ToString(),
                Quadrants = this.L1.GetQuadrants()
            };

            this.dash2 = new Dashboard
            {
                Name = this.L2.ToString(),
                Quadrants = this.L2.GetQuadrants()
            };

            this.dash3 = new Dashboard
            {
                Name = this.L3.ToString(),
                Quadrants = this.L3.GetQuadrants()
            };

            this.dash4 = new Dashboard
            {
                Name = this.L4.ToString(),
                Quadrants = this.L4.GetQuadrants()
            };

            this.dash5 = new Dashboard
            {
                Name = this.L5.ToString(),
                Quadrants = this.L5.GetQuadrants()
            };

            this.dash6 = new Dashboard
            {
                Name = this.L6.ToString(),
                Quadrants = this.L6.GetQuadrants()
            };

            db.Add(this.dash1);
            db.Add(this.dash2);
            db.Add(this.dash3);
            db.Add(this.dash4);
            db.Add(this.dash5);
            db.Add(this.dash6);

            this.project.Dashboards = db.ToArray();
        }

        /// <summary>
        /// Mouse click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnRemoveURL_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgURLs.SelectedIndex > -1)
            {
                int index = this.dgURLs.SelectedIndex;
                List<Model.Url> lstUrl = this.project.Urls.ToList();
                lstUrl.RemoveAt(index);
                this.project.Urls = lstUrl.ToArray();
                this.dgURLs.ItemsSource = this.project.Urls;
            }
        }

        /// <summary>
        /// Mouse click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnAddURLList_Click(object sender, RoutedEventArgs e)
        {
            int tmr = 0;

            if (this.txtRegion.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show(this, "The dashboard region must be selected!", Properties.Resources.SysName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                this.txtRegion.Focus();
                return;
            }

            if (this.cboURLs.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show(this, "The URL must be selected!", Properties.Resources.SysName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                this.cboURLs.Focus();
                return;
            }

            if (!int.TryParse(this.txtTimer.Text, out tmr))
            {
                MessageBox.Show(this, "The timer value is invalid!", Properties.Resources.SysName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                this.txtTimer.Focus();
                return;
            }

            Url url = new Url { Name = this.cboURLs.Text, Time = Convert.ToInt32(this.txtTimer.Text) };

            this.AddURLToControl(url);
        }

        /// <summary>
        /// Add the URL to the control.
        /// </summary>
        /// <param name="url">Url to add.</param>
        private void AddURLToControl(Url url)
        {
            switch (this.txtRegion.Text.Trim())
            {
                // LAYOUT 1
                case "L1-A":
                    this.L1.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.A);
                    break;
                case "L1-B":
                    this.L1.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.B);
                    break;
                case "L1-C":
                    this.L1.AddURL(url, QuadrantName.C);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.C);
                    break;
                case "L1-D":
                    this.L1.AddURL(url, QuadrantName.D);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.D);
                    break;

                // LAYOUT 2
                case "L2-A":
                    this.L2.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L2.GetURLs(QuadrantName.A);
                    break;

                // LAYOUT 3
                case "L3-A":
                    this.L3.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.A);
                    break;
                case "L3-B":
                    this.L3.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.B);
                    break;
                case "L3-C":
                    this.L3.AddURL(url, QuadrantName.C);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.C);
                    break;

                // LAYOUT 4
                case "L4-A":
                    this.L4.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.A);
                    break;
                case "L4-B":
                    this.L4.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.B);
                    break;

                // LAYOUT 5
                case "L5-A":
                    this.L5.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.A);
                    break;
                case "L5-B":
                    this.L5.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.B);
                    break;

                // LAYOUT 5
                case "L6-A":
                    this.L6.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.A);
                    break;
                case "L6-B":
                    this.L6.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.B);
                    break;
                case "L6-C":
                    this.L6.AddURL(url, QuadrantName.C);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.C);
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// Add the URL to the control.
        /// </summary>
        /// <param name="url">Url to add.</param>
        private void AddURLToControl(Url[] url)
        {
            switch (this.txtRegion.Text.Trim())
            {
                // LAYOUT 1
                case "L1-A":
                    this.L1.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.A);
                    break;
                case "L1-B":
                    this.L1.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.B);
                    break;
                case "L1-C":
                    this.L1.AddURL(url, QuadrantName.C);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.C);
                    break;
                case "L1-D":
                    this.L1.AddURL(url, QuadrantName.D);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.D);
                    break;

                // LAYOUT 2
                case "L2-A":
                    this.L2.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L2.GetURLs(QuadrantName.A);
                    break;

                // LAYOUT 3
                case "L3-A":
                    this.L3.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.A);
                    break;
                case "L3-B":
                    this.L3.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.B);
                    break;
                case "L3-C":
                    this.L3.AddURL(url, QuadrantName.C);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.C);
                    break;

                // LAYOUT 4
                case "L4-A":
                    this.L4.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.A);
                    break;
                case "L4-B":
                    this.L4.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.B);
                    break;

                // LAYOUT 5
                case "L5-A":
                    this.L5.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.A);
                    break;
                case "L5-B":
                    this.L5.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.B);
                    break;

                // LAYOUT 5
                case "L6-A":
                    this.L6.AddURL(url, QuadrantName.A);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.A);
                    break;
                case "L6-B":
                    this.L6.AddURL(url, QuadrantName.B);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.B);
                    break;
                case "L6-C":
                    this.L6.AddURL(url, QuadrantName.C);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.C);
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// Method to load the window.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.InitializeNewProject();

            this.L1.ChangedA += new MouseLeftButtonUpEventHandler(this.L1_ChangedA);
            this.L1.ChangedB += new MouseLeftButtonUpEventHandler(this.L1_ChangedB);
            this.L1.ChangedC += new MouseLeftButtonUpEventHandler(this.L1_ChangedC);
            this.L1.ChangedD += new MouseLeftButtonUpEventHandler(this.L1_ChangedD);
            this.L2.ChangedA += new MouseLeftButtonUpEventHandler(this.L2_ChangedA);
            this.L3.ChangedA += new MouseLeftButtonUpEventHandler(this.L3_ChangedA);
            this.L3.ChangedB += new MouseLeftButtonUpEventHandler(this.L3_ChangedB);
            this.L3.ChangedC += new MouseLeftButtonUpEventHandler(this.L3_ChangedC);
            this.L4.ChangedA += new MouseLeftButtonUpEventHandler(this.L4_ChangedA);
            this.L4.ChangedB += new MouseLeftButtonUpEventHandler(this.L4_ChangedB);
            this.L5.ChangedA += new MouseLeftButtonUpEventHandler(this.L5_ChangedA);
            this.L5.ChangedB += new MouseLeftButtonUpEventHandler(this.L5_ChangedB);
            this.L6.ChangedA += new MouseLeftButtonUpEventHandler(this.L6_ChangedA);
            this.L6.ChangedB += new MouseLeftButtonUpEventHandler(this.L6_ChangedB);
            this.L6.ChangedC += new MouseLeftButtonUpEventHandler(this.L6_ChangedC);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L6_ChangedC(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L6-C";
            this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.C);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L6_ChangedB(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L6-B";
            this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.B);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L6_ChangedA(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L6-A";
            this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.A);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L5_ChangedB(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L5-B";
            this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.B);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L5_ChangedA(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L5-A";
            this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.A);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L4_ChangedB(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L4-B";
            this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.B);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L4_ChangedA(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L4-A";
            this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.A);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L3_ChangedC(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L3-C";
            this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.C);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L3_ChangedB(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L3-B";
            this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.B);
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void L3_ChangedA(object sender, EventArgs e)
        {
            this.txtRegion.Text = "L3-A";
            this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.A);
        }

        /// <summary>
        /// Window activated event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void Window_Activated(object sender, EventArgs e)
        {
            if (this.tabSetupURL.IsSelected)
            {
                if (!this.isWindowActived)
                {
                    if (Clipboard.GetText().StartsWith("http"))
                    {
                        this.txtURL.Text = Clipboard.GetText();
                    }
                }
            }

            this.isWindowActived = true;
        }

        /// <summary>
        /// Window deactivated event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void Window_Deactivated(object sender, EventArgs e)
        {
            this.isWindowActived = false;
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnRemoveURLList_Click(object sender, RoutedEventArgs e)
        {
            int index = this.dgURLsDashboadr.SelectedIndex;

            this.RemoveURLFromControl(index);
        }

        /// <summary>
        /// This method removes the URL from the control.
        /// </summary>
        /// <param name="index">Index URL to be removed.</param>
        private void RemoveURLFromControl(int index)
        {
            switch (this.txtRegion.Text.Trim())
            {
                // LAYOUT 1
                case "L1-A":
                    this.L1.RemoveURL(QuadrantName.A, index);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.A);
                    break;
                case "L1-B":
                    this.L1.RemoveURL(QuadrantName.B, index);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.B);
                    break;
                case "L1-C":
                    this.L1.RemoveURL(QuadrantName.C, index);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.C);
                    break;
                case "L1-D":
                    this.L1.RemoveURL(QuadrantName.D, index);
                    this.dgURLsDashboadr.ItemsSource = this.L1.GetURLs(QuadrantName.D);
                    break;

                // LAYOUT 2
                case "L2-A":
                    this.L2.RemoveURL(QuadrantName.A, index);
                    this.dgURLsDashboadr.ItemsSource = this.L2.GetURLs(QuadrantName.A);
                    break;

                // LAYOUT 3
                case "L3-A":
                    this.L3.RemoveURL(QuadrantName.A, index);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.A);
                    break;
                case "L3-B":
                    this.L3.RemoveURL(QuadrantName.B, index);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.B);
                    break;
                case "L3-C":
                    this.L3.RemoveURL(QuadrantName.C, index);
                    this.dgURLsDashboadr.ItemsSource = this.L3.GetURLs(QuadrantName.C);
                    break;

                // LAYOUT 4
                case "L4-A":
                    this.L4.RemoveURL(QuadrantName.A, index);
                    this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.A);
                    break;
                case "L4-B":
                    this.L4.RemoveURL(QuadrantName.B, index);
                    this.dgURLsDashboadr.ItemsSource = this.L4.GetURLs(QuadrantName.B);
                    break;

                // LAYOUT 5
                case "L5-A":
                    this.L5.RemoveURL(QuadrantName.A, index);
                    this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.A);
                    break;
                case "L5-B":
                    this.L5.RemoveURL(QuadrantName.B, index);
                    this.dgURLsDashboadr.ItemsSource = this.L5.GetURLs(QuadrantName.B);
                    break;

                // LAYOUT 5
                case "L6-A":
                    this.L6.RemoveURL(QuadrantName.A, index);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.A);
                    break;
                case "L6-B":
                    this.L6.RemoveURL(QuadrantName.B, index);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.B);
                    break;
                case "L6-C":
                    this.L6.RemoveURL(QuadrantName.C, index);
                    this.dgURLsDashboadr.ItemsSource = this.L6.GetURLs(QuadrantName.C);
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void Image_Drop(object sender, DragEventArgs e)
        {
            this.AddDashboardToTimeline();
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnPlayer_Click(object sender, RoutedEventArgs e)
        {
            this.ShowPresenterPlayer();
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void ImgPlay_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.ShowPresenterPlayer();
        }

        /// <summary>
        /// Shows the presenter player.
        /// </summary>
        private void ShowPresenterPlayer()
        {
            if (!this.cboWindowType.Text.Equals(string.Empty))
            {
                PresenterPayer pplay = new PresenterPayer(this.project);

                pplay.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
                pplay.ShowInTaskbar = false;

                switch (this.cboWindowType.Text)
                {
                    case "Full Screen":
                        pplay.WindowStyle = System.Windows.WindowStyle.None;
                        pplay.WindowState = System.Windows.WindowState.Maximized;
                        pplay.Focus();
                        pplay.ShowDialog();
                        break;

                    case "Normal Screen":
                        pplay.WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
                        pplay.WindowState = System.Windows.WindowState.Maximized;
                        pplay.Focus();
                        pplay.ShowDialog();
                        break;

                    default:
                        break;
                }

                if (pplay != null)
                {
                    pplay.Close();
                    pplay = null;
                }
            }
            else
            {
                MessageBox.Show(this, "Select the 'Open Player As...' option!", Properties.Resources.SysName, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                this.cboWindowType.Focus();
            }
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void TabPresenterPlayer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AnimateMenu(sender);
            this.txtProjectNameSummary.Text = this.project.Name;
            this.txtUserNameSummary.Text = this.project.UserName;

            if (this.lstDashBoardTimeLine.Count <= 0)
            {
                this.imgPlay.IsEnabled = false;
                this.BtnPlayer.IsEnabled = false;
                return;
            }

            foreach (Dashboard item in this.lstDashBoardTimeLine)
            {
                if (!this.VerifyLayoutContent(item.Name, false))
                {
                    this.imgPlay.IsEnabled = false;
                    this.BtnPlayer.IsEnabled = false;
                    return;
                }
            }

            this.imgPlay.IsEnabled = true;
            this.BtnPlayer.IsEnabled = true;
            this.LoadListDashboardSummary();
        }

        /// <summary>
        /// Method to add dashboards to the timeline.
        /// </summary>
        private void LoadListDashboardSummary()
        {
            ArrayList lstLayoutClone = new ArrayList();
            ArrayList lstLayoutCloneText = new ArrayList();

            TextBlock text = null;

            if (this.project.TimeLine.Dashboards != null)
            {
                foreach (var item in this.project.TimeLine.Dashboards)
                {
                    Type tp = Type.GetType(item.Name);
                    object lay = Activator.CreateInstance(tp);
                    text = new TextBlock();

                    string ctxTexto = this.GetDashboardSummary(item);

                    text.Width = 100;
                    text.Height = 100;
                    text.Text = ctxTexto;

                    lstLayoutClone.Add(lay);
                    lstLayoutCloneText.Add(text);
                }

                this.lstPathSummary_Copy.ItemsSource = null;
                this.lstPathSummary_Copy.ItemsSource = lstLayoutCloneText;

                this.lstPathSummary.ItemsSource = null;
                this.lstPathSummary.ItemsSource = lstLayoutClone;
            }
        }

        /// <summary>
        /// Methdo to get Summary data.
        /// </summary>
        /// <param name="item">Dashbord to get the data.</param>
        /// <returns>Summary data.</returns>
        private string GetDashboardSummary(Dashboard item)
        {
            string sum = string.Empty;
            int a, b, c, d, total;

            switch (item.Name)
            {
                // LAYOUT 1
                case "Ultra_Presenter.UC.Layout_1":
                    a = this.L1.GetURLs(QuadrantName.A).Length;
                    b = this.L1.GetURLs(QuadrantName.B).Length;
                    c = this.L1.GetURLs(QuadrantName.C).Length;
                    d = this.L1.GetURLs(QuadrantName.D).Length;
                    total = this.L1.GetTotalTime();

                    sum = "A = " + (a > 1 ? a.ToString() + " Urls\n" : a.ToString() + " Url\n");
                    sum += "B = " + (b > 1 ? b.ToString() + " Urls\n" : b.ToString() + " Url\n");
                    sum += "C = " + (c > 1 ? c.ToString() + " Urls\n" : c.ToString() + " Url\n");
                    sum += "D = " + (d > 1 ? d.ToString() + " Urls\n" : d.ToString() + " Url\n");
                    sum += "\n";
                    sum += "Total = " + total.ToString() + " sec.";
                    break;

                // LAYOUT 2
                case "Ultra_Presenter.UC.Layout_2":
                    a = this.L2.GetURLs(QuadrantName.A).Length;
                    total = this.L2.GetTotalTime();

                    sum = "A = " + (a > 1 ? a.ToString() + " Urls\n" : a.ToString() + " Url\n");
                    sum += "\n";
                    sum += "\n";
                    sum += "\n";
                    sum += "\n";
                    sum += "Total = " + total.ToString() + " sec.";

                    break;

                // LAYOUT 3
                case "Ultra_Presenter.UC.Layout_3":
                    a = this.L3.GetURLs(QuadrantName.A).Length;
                    b = this.L3.GetURLs(QuadrantName.B).Length;
                    c = this.L3.GetURLs(QuadrantName.C).Length;
                    total = this.L3.GetTotalTime();

                    sum = "A = " + (a > 1 ? a.ToString() + " Urls\n" : a.ToString() + " Url\n");
                    sum += "B = " + (b > 1 ? b.ToString() + " Urls\n" : b.ToString() + " Url\n");
                    sum += "C = " + (c > 1 ? c.ToString() + " Urls\n" : c.ToString() + " Url\n");
                    sum += "\n";
                    sum += "\n";
                    sum += "Total = " + total.ToString() + " sec.";
                    break;

                // LAYOUT 4
                case "Ultra_Presenter.UC.Layout_4":
                    a = this.L4.GetURLs(QuadrantName.A).Length;
                    b = this.L4.GetURLs(QuadrantName.B).Length;
                    total = this.L4.GetTotalTime();

                    sum = "A = " + (a > 1 ? a.ToString() + " Urls\n" : a.ToString() + " Url\n");
                    sum += "B = " + (b > 1 ? b.ToString() + " Urls\n" : b.ToString() + " Url\n");
                    sum += "\n";
                    sum += "\n";
                    sum += "\n";
                    sum += "Total = " + total.ToString() + " sec.";
                    break;

                // LAYOUT 5
                case "Ultra_Presenter.UC.Layout_5":
                    a = this.L5.GetURLs(QuadrantName.A).Length;
                    b = this.L5.GetURLs(QuadrantName.B).Length;
                    total = this.L5.GetTotalTime();

                    sum = "A = " + (a > 1 ? a.ToString() + " Urls\n" : a.ToString() + " Url\n");
                    sum += "B = " + (b > 1 ? b.ToString() + " Urls\n" : b.ToString() + " Url\n");
                    sum += "\n";
                    sum += "\n";
                    sum += "\n";
                    sum += "Total = " + total.ToString() + " sec.";
                    break;

                // LAYOUT 6
                case "Ultra_Presenter.UC.Layout_6":
                    a = this.L6.GetURLs(QuadrantName.A).Length;
                    b = this.L6.GetURLs(QuadrantName.B).Length;
                    c = this.L6.GetURLs(QuadrantName.C).Length;
                    total = this.L6.GetTotalTime();

                    sum = "A = " + (a > 1 ? a.ToString() + " Urls\n" : a.ToString() + " Url\n");
                    sum += "B = " + (b > 1 ? b.ToString() + " Urls\n" : b.ToString() + " Url\n");
                    sum += "C = " + (c > 1 ? c.ToString() + " Urls\n" : c.ToString() + " Url\n");
                    sum += "\n";
                    sum += "\n";
                    sum += "Total = " + total.ToString() + " sec.";
                    break;

                default:
                    break;
            }

            return sum;
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void LnkSobreURL_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnUp_Click(object sender, RoutedEventArgs e)
        {
            int linhaPos = this.dgURLsDashboadr.SelectedIndex;

            if (linhaPos > 0)
            {
                Url selectedUrl = (Url)this.dgURLsDashboadr.SelectedItem;

                List<Url> urls = new List<Url>((Url[])this.dgURLsDashboadr.ItemsSource);

                urls.RemoveAt(linhaPos);
                urls.Insert(linhaPos - 1, selectedUrl);

                this.AddURLToControl(urls.ToArray());
            }
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void BtnDown_Click(object sender, RoutedEventArgs e)
        {
            int linhaPos = this.dgURLsDashboadr.SelectedIndex;

            if (linhaPos >= 0 && this.dgURLsDashboadr.Items.Count > linhaPos + 1)
            {
                Url selectedUrl = (Url)this.dgURLsDashboadr.SelectedItem;

                List<Url> urls = new List<Url>((Url[])this.dgURLsDashboadr.ItemsSource);

                urls.RemoveAt(linhaPos);
                urls.Insert(linhaPos + 1, selectedUrl);

                this.AddURLToControl(urls.ToArray());
            }
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void TabGenProject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AnimateMenu(sender);
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void TabSetupURL_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AnimateMenu(sender);
        }

        /// <summary>
        /// Click event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void TabSetupTimeLine_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            AnimateMenu(sender);
        }
    }
}
